package ym.controller.front.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ym.pojo.User;
import ym.service.MessageService;
import ym.service.UserService;
import ym.utils.RandomUtil;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 用户接口
 */
@RestController
@CrossOrigin
@RequestMapping("/front/user")
public class FrontUserCon {
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(User user) {
        userService.add(user);
        return BaseResult.ok();
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(User user) {
        userService.update(user);
        return BaseResult.ok();
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer userId) {
        userService.delete(userId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询用户
     *
     * @param userId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer userId) {
        User user = userService.findById(userId);
        return BaseResult.ok(user);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<User> all = userService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询用户
     *
     * @param page
     * @param size
     * @return
     */

    @GetMapping("/search")
    public BaseResult search(int page, int size) {
        Page<User> brandPage = userService.search(page, size);
        return BaseResult.ok(brandPage);
    }


    /**
     * 发送注册短信
     *
     * @param phone 注册手机号
     * @return
     */
    @GetMapping("/sendMessage")
    public BaseResult sendMessage(String phone) {
        // 1.生成随机四位数
        String checkCode = RandomUtil.buildCheckCode(4);
        // 2.发送短信
        BaseResult result = messageService.sendMessage(phone, checkCode);
        // 3.发送成功，将验证码保存到redis中,发送失败，返回发送结果
        if (200 == result.getCode()) {
            userService.saveRegisterCheckCode(phone, checkCode);
            return BaseResult.ok();
        } else {
            return result;
        }
    }

    /**
     * 验证用户注册验证码
     *
     * @param phone     手机号
     * @param checkCode 验证码
     * @return
     */
    @GetMapping("/registerCheckCode")
    public BaseResult register(String phone, String checkCode) {
        userService.registerCheckCode(phone, checkCode);
        return BaseResult.ok();
    }

    /**
     * 注册用户
     *
     * @param user 用户信息
     * @return
     */
    @PostMapping("/register")
    public BaseResult register(User user) {
        userService.register(user);
        return BaseResult.ok();
    }


    /**
     * 发送登录短信
     *
     * @param phone 手机号
     * @return 验证码
     */
    @GetMapping("/sendLoginCheckCode")
    public BaseResult sendLoginCheckCode(String phone) {
        // 1.生成随机四位数
        String checkCode = RandomUtil.buildCheckCode(4);
        // 2.发送短信
        BaseResult result = messageService.sendMessage(phone, checkCode);
        // 3.发送成功，将验证码保存到redis中,发送失败，返回发送结果
        if (200 == result.getCode()) {
            userService.saveLoginCheckCode(phone, checkCode);
            return BaseResult.ok();
        } else {
            return result;
        }
    }

    /**
     * 手机号验证码登录
     *
     * @param phone     手机号
     * @param checkCode 验证码
     * @return token
     */
    @PostMapping("/loginCheckCode")
    public BaseResult loginCheckCode(String phone, String checkCode) {
        String token = userService.loginCheckCode(phone, checkCode);
        return BaseResult.ok(token);
    }

    /**
     * 用户名密码登录
     *
     * @param user 用户对象
     * @return token
     */
    @PostMapping("/loginPassword")
    public BaseResult loginPassword(User user) {
        String token = userService.loginPassword(user.getUsername(), user.getPassword());
        return BaseResult.ok(token);
    }

    /**
     * 根据token获取登录用户
     *
     * @param token 令牌
     * @return 用户
     */
    @GetMapping("/getUser")
    public BaseResult getUser(String token) {
        User u = userService.getLoginUser(token);
        return BaseResult.ok(u);
    }
}
